package com.example.vv.model.accountItem;

import android.content.Context;
import android.view.View;

import com.example.vv.R;
import com.example.vv.fragmentWork.entity.WxarticleEntity;
import com.example.vv.fragmentWork.mvp.MvpFragment;
import com.example.vv.model.accountItem.base.adapter.AIAdapter;
import com.example.vv.model.accountItem.base.contract.AccountItemContract;
import com.example.vv.model.accountItem.base.presenter.AccountItemPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;


public class AIFragment extends MvpFragment<AccountItemContract.View, AccountItemPresenter> implements AccountItemContract.View {
    int id;
    int pager = 1;
    @BindView(R.id.ai_recycler)
    RecyclerView aiRecycler;
    @BindView(R.id.ai_smartRefresh)
    SmartRefreshLayout aiSmartRefresh;
    AIAdapter aiAdapter;

    public AIFragment(int id) {
        this.id = id;
    }

    @Override
    public int initLayout() {
        return R.layout.fragment_a_i;
    }

    @Override
    public AccountItemPresenter initPresenter() {
        return new AccountItemPresenter();
    }

    @Override
    public void initView() {
        aiSmartRefresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pager++;
                presenter.setWXList("wxarticle/list/" + id + "/" + pager + "/json");
                refreshLayout.finishLoadMore(1000);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                if (aiAdapter!=null) {
                    aiAdapter.clear();
                    pager=1;
                    presenter.setWXList("wxarticle/list/" + id + "/1/json");
                }
                refreshLayout.finishRefresh(1000);
            }
        });
        aiSmartRefresh.autoRefresh();
        aiAdapter = new AIAdapter(getActivity());
        aiRecycler.setAdapter(aiAdapter);
    }

    @Override
    public void lazyLoad() {
        presenter.setWXList("wxarticle/list/" + id + "/1/json");
    }

    @Override
    public void initData() {

    }


    @Override
    public void doBusiness() {

    }

    @Override
    public View titleBar() {
        return null;
    }


    @Override
    public Context getMContext() {
        return getActivity();
    }


    @Override
    public void getWXList(WxarticleEntity wxarticleEntity) {
        aiAdapter.addData(wxarticleEntity.getDatas());
    }
}